﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;


namespace BGCOP.CAVTOP.BUSINESSLAYER
{
    public class logIn
    {
        public usrDB CompruebaLogin(string usuario, string contra)
        {
            usrDB res = new usrDB();
            res.idUsr = -1;
            masterDBACEntities db = new masterDBACEntities();
            byte[] cpp = GetMd5Hash(contra);
            var tu = (from usu in db.TUsuario 
                           join ts in db.TTipoUsuario 
                           on usu.idTipoUsuario  equals ts.idTipoUsuario  
                           where usu.login == usuario && usu.password.Equals(cpp) == true select new
                                                                                                     {
                                                                                                         idusr=usu.idUsuario,
                                                                                                       permi=ts.permisos 
                                                                                                     }).FirstOrDefault();

            if (tu != null)
            {
                res.idUsr = tu.idusr;
                res.permiUsr = tu.permi;
            }
            return res;
        }
        private byte[] GetMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            return data;
        }
    }
    public class usrDB
    {
        public int idUsr { get; set; }
        public string permiUsr { get; set; }
    }
}
